﻿using AntdUI;
using AntdUI.In;
using Microsoft.Office.Interop.Excel;
using PPSMS.CommonForm;
using PPSMS.Controllers;
using PPSMS_BLL;
using PPSMS_Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PPSMS;

public partial class DeviceManagement : UIPage
{
    InverterDevices_BLL _BLL = new InverterDevices_BLL();

    public List<string> models = new();

    public List<string> manufacturers = new();

    private StationStatus_BLL status_BLL = new StationStatus_BLL();

    public event System.Action OnRefres;

    public static Action<long> OnDeleteRefresh;

    public DeviceManagement()
    {
        InitializeComponent();
    }

    private void DeviceManagement_Load(object sender, EventArgs e)
    {
        ON_Refres();
    }

    /// <summary>
    /// 绑定下拉框
    /// </summary>
    private void BindDropdownMenu()
    {
        var manufacturer = _BLL.Obtain_Manufacturer();
        models.Clear();
        manufacturers.Clear();
        select1.Items.Clear();
        select2.Items.Clear();
        foreach (var i in manufacturer)
        {
            select1.Items.Add(i);
            manufacturers.Add(i);
        }

        var model = _BLL.Obtain_Model();
        foreach (var i in model)
        {
            select2.Items.Add(i);
            models.Add(i);
        }
    }

    /// <summary>
    /// 查询设备
    /// </summary>
    public void QueryDevice(List<InverterDevices> obj)
    {
        uiFlowLayoutPanel1.FlowLayoutPanel.Controls.Clear();
        foreach (var device in obj)
        {
            var inverter = new DeviceControl(device);
            inverter.ContextMenuStrip = contextMenuStrip1;
            uiFlowLayoutPanel1.Controls.Add(inverter);
        }
    }
    public void QueryDevice()
    {
        var inverters = _BLL.SelectAllDevices();
        uiFlowLayoutPanel1.FlowLayoutPanel.Controls.Clear();
        foreach (var device in inverters)
        {
            var inverter = new DeviceControl(device);
            inverter.ContextMenuStrip = contextMenuStrip1;
            uiFlowLayoutPanel1.Controls.Add(inverter);
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string manufacturer = select1.Text;
        string model = select2.Text;
        List<InverterDevices> inverterDevices = null;

        if (!manufacturers.Contains(manufacturer) && !models.Contains(model))
        {
            ON_Refres();          
        }

        if (models.Contains(model) && manufacturers.Contains(manufacturer))
        {
            inverterDevices = _BLL.SelectByModel_Manufacturer(manufacturer, model);
        }
        else if (models.Contains(model))
        {
            inverterDevices = _BLL.SelectByModel(model);
        }
        else if (manufacturers.Contains(manufacturer))
        {
            inverterDevices = _BLL.SelectByManufacturer(manufacturer);
        }

        if (inverterDevices != null)
        {
            BindDropdownMenu();
            QueryDevice(inverterDevices);
        }

    }

    private void ON_Refres()
    {
        BindDropdownMenu();
        QueryDevice();
    }

    private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
    {
        // 得到当前点击的控件对象
        Control control = contextMenuStrip1.SourceControl;
        var sf = control as DeviceControl;
        if (sf != null)
        {
            contextMenuStrip1.Items.Clear();
            contextMenuStrip1.Items.Add(delMenu);
            contextMenuStrip1.Items.Add(updateMenu);
        }
        else
        {
            contextMenuStrip1.Items.Clear();
            contextMenuStrip1.Items.Add(addMenu);
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        var s = new AddInverterForm();
        s.OnRefres += ON_Refres;
        s.ShowDialog();
    }

    private void addMenu_Click(object sender, EventArgs e)
    {
        var s = new AddInverterForm();
        s.OnRefres += ON_Refres;
        s.ShowDialog();
    }

    private void updateMenu_Click(object sender, EventArgs e)
    {
        Control control = contextMenuStrip1.SourceControl;
        var inverter = control as DeviceControl;
        if (inverter != null)
        {
            InverterDevices device = inverter._inverter;
            var s = new AddInverterForm(device);
            s.OnRefres += ON_Refres;
            s.ShowDialog();
        }
    }

    private void delMenu_Click(object sender, EventArgs e)
    {
        Control control = contextMenuStrip1.SourceControl;
        var inverter = control as DeviceControl;
        if (inverter != null)
        {
            long id = inverter.InverterID;
            if (MessageBox.Show("确定删除？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK)
            {
                _BLL.Delete(id);
                status_BLL.UpdateCount(false);
                OnDeleteRefresh.Invoke(id);
                BindDropdownMenu();
                ON_Refres();
                
            }
        }
    }


}
